﻿using BellAssistant.Common.CommonHelper;
using BellAssistant.DSS;
using BellAssistant.Model.Enum;
using BellAssistant.Model.Tables;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace BellAssistant.DataManager
{
    public partial class PromotionManage : FrmBase
    {
        public PromotionManage()
        {
            InitializeComponent();
            btnAdd.Click += BtnAdd_Click;
            //导出
            btn_OutPut.Click += btn_OutPut_Click;
            //查询
            btnSearch.Click += btnSearch_Click;
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            List<PromotionType> list = CreateDataSourceService.IDataSourceService.GetAllPromotionType();
            gridControl1.DataSource = list;
        }

        private void btn_OutPut_Click(object sender, EventArgs e)
        {
            try
            {
                GridViewHelper.ExportToExcel(gridView1, null);
            }
            catch (Exception ex)
            {
                LogHelper.WriteLog.Error(ex.Message);
            }
        }

        private void BtnAdd_Click(object sender, EventArgs e)
        {
            PromotionType promotionType = new PromotionType();

            string str = IsINputStempyValue();
            if (str == "")
            {
                promotionType.PromotionName = txtPromotionName.Text;
                promotionType.PromotionPeople = ((PromotionPeople)radioPeople.SelectedIndex).GetDescription();
                promotionType.PromotionAction = ((ExpressEnum)radioAction.SelectedIndex).GetDescription();
                promotionType.PromotionStatus = PromotionStatus.Prosess.GetDescription();

                //promotionType.PromotionPeople = radioPeople.Text;
                //promotionType.PromotionAction = radioAction.Text;
                //promotionType.PromotionStatus = "进行中";


                promotionType.StartTime = startTime.DateTime;
                promotionType.EndTime = endTime.DateTime;
                promotionType.PromotionTag = txtTag.Text;
                promotionType.CouponID = cmbCoupons.SelectedIndex + 1;

                string result = CreateDataSourceService.IDataSourceService.SavePromotionType(promotionType);
                MessageBox.Show(result, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                ClearAll();
                btnSearch_Click(sender, e);
            }
            else
            {
                MessageBox.Show(str, "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

        private void ClearAll()
        {
            txtPromotionName.Text = string.Empty;
            txtTag.Text = string.Empty;
            startTime.Text = string.Empty;
            endTime.Text = string.Empty;
        }

        private string IsINputStempyValue()
        {
            string result = "";
            if (string.IsNullOrEmpty(txtPromotionName.Text) || string.IsNullOrEmpty(txtTag.Text) || string.IsNullOrEmpty(cmbPromotionStatus.Text) || string.IsNullOrEmpty(cmbCoupons.Text))
            {
                result = "信息不能为空！";
            }
            if (startTime.DateTime >= endTime.DateTime)
            {
                result = "活动开始时间不能大于等于结束时间！";
            }
            if ((endTime.DateTime - startTime.DateTime).Days >= 14)
            {
                result = "活动时间不能大于14天！";
            }
            return result;
        }
    }
}
